package jp.sourceforge.akjrcp.dropsomething;

import java.io.File;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.commons.ui.FileDirChoicer;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeEvent;
import jp.sourceforge.akjrcp.commons.ui.AbstractFileDirChoicer.FileStatusChangeListener;
import jp.sourceforge.akjrcp.ui.FileOverwriteControler;
import jp.sourceforge.akjrcp.ui.LayoutBuilder;
import jp.sourceforge.akjrcp.ui.FileOverwriteControler.AskOverwrite;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PlatformUI;

public abstract class DirectoryBasedDropSomething extends DropsomethingTab{


	public   DirectoryBasedDropSomething(Composite parent, int style,String key) {
		super(parent, style,key);
		setProcessPrinter(Akj_betasPlugin.getDefault().getProcessPrinter());
	}
	
	public   DirectoryBasedDropSomething(Composite parent, int style) {
		this(parent, style,null);
	}


	protected Button samefolder,backup;
	protected FileDirChoicer directoryChooser;
	
	
	/**
	 * @deprecated  own overwritecontroler;
	 */
	private FileOverwriteControler overwriteControl;
	/*
	 * Directory control
	 */
	
	
	public abstract IPreferenceStore getPreferenceStore();
	public void createDirectoryInfo(Composite parent){
		Composite savedirs=new Composite(parent,SWT.NULL);
		GridData gdata=new GridData(GridData.FILL_HORIZONTAL);
		savedirs.setLayoutData(gdata);
		GridLayout layout=new GridLayout(3,false);
		savedirs.setLayout(LayoutBuilder.makeGridLayoutNoMargin(3,false));
		
		//check same folder
		samefolder = new Button(savedirs,SWT.CHECK);
		samefolder.setText(Messages.getString("textinsert.samefolder"));
		samefolder.addSelectionListener(new SelectionListener(){

			public void widgetSelected(SelectionEvent e) {
				sameFolderSelected();
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		IPreferenceStore preference=getPreferenceStore();
		preference.setDefault(getPreferenceKey()+".directoryinfo.samefolder", true);
		preference.setDefault(getPreferenceKey()+".directoryinfo.backup", true);
		preference.setDefault(getPreferenceKey()+".directoryinfo.directory", "");
		
		
		//directory choose
		directoryChooser = new FileDirChoicer(savedirs,SWT.NULL,FileDirChoicer.DIR,Messages.getString("textinsert.exportfolder"));
		String directoryPath=preference.getString(getPreferenceKey()+".directoryinfo.directory");
		if(!new File(directoryPath).exists()){// exist path only.
			directoryPath="";
		}
		directoryChooser.setFilePath(directoryPath); //restore
		directoryChooser.addFileListChangeListener(new FolderChoosed());
		directoryChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		//backup is maybe option
		backup = new Button(savedirs,SWT.CHECK);
		backup.setText(Messages.getString("textinsert.backup"));
		backup.setEnabled(true);//should preference
		backup.setSelection(preference.getBoolean(getPreferenceKey()+".directoryinfo.backup"));
		
		//default v
		samefolder.setSelection(preference.getBoolean(getPreferenceKey()+".directoryinfo.samefolder"));
		sameFolderSelected();
		
		//restore default value.
		SaveDirectoryInfoPreferenceStore ss=new SaveDirectoryInfoPreferenceStore();
		
		directoryChooser.addFileListChangeListener(ss);
		backup.addSelectionListener(ss);
		samefolder.addSelectionListener(ss);
		
	}
	
	public class SaveDirectoryInfoPreferenceStore implements SelectionListener,FileStatusChangeListener{
		
		public void widgetDefaultSelected(SelectionEvent e) {
			
		}

		public void widgetSelected(SelectionEvent e) {
			Control control=(Control) e.getSource();
			if(control==backup){
				
				getPreferenceStore().setValue(getPreferenceKey()+".directoryinfo.backup", backup.getSelection());
					
				}
			else if(control==samefolder){
				
				getPreferenceStore().setValue(getPreferenceKey()+".directoryinfo.samefolder", samefolder.getSelection());
					
				}
		}

		public void addList(FileStatusChangeEvent event) {
			
			//if(event.getSource()==directoryChooser){ //some problem these code,i don know
				getPreferenceStore().setValue(getPreferenceKey()+".directoryinfo.directory", directoryChooser.getAbsoluteFilePath());
				//log.info(directoryChooser.getAbsoluteFilePath());
			//}
		}

		public void removeList(FileStatusChangeEvent event) {
			//if(event.getSource()==directoryChooser){
				getPreferenceStore().setValue(getPreferenceKey()+".directoryinfo.directory", directoryChooser.getAbsoluteFilePath());
				
			//}
		}
	}
	
	public boolean isBackup(){
		return backup.getSelection();
	}
	
	public boolean isSameFolder(){
		return samefolder.getSelection();
	}
	
	public String getChoosedPath(){
		return directoryChooser.getAbsoluteFilePath();
	}
	
	public void sameFolderSelected(){
		if(samefolder.getSelection()){
			directoryChooser.setEnabled(false);
		}else{
			directoryChooser.setEnabled(true);
			
		}
		updateStatus();//should implement
	}
	
	public class FolderChoosed implements FileStatusChangeListener{

		public void removeList(FileStatusChangeEvent event) {
			// TODO Auto-generated method stub
			
		}

		public void addList(FileStatusChangeEvent event) {
			updateStatus();
		}
		
	}
	
	public boolean askOverwrite(File output){
		
		AskOverwrite ask=new AskOverwrite(overwriteControl,output);
		PlatformUI.getWorkbench().getDisplay().syncExec(ask);
		return ask.isOverwrite();
	}
	
	

	public abstract void updateStatus();

	public void initConvert(){
		if(getViewPart()==null){
			log.fatal("not set View Part in createTab()");
		}
		IViewSite viewSite = getViewPart().getViewSite();
		
		overwriteControl=new FileOverwriteControler(viewSite.getShell(),FileOverwriteControler.MODE_YES_YES_ALL_NO);//for easy
	}
	
	public void releaseConvert(){
		overwriteControl=null;
	}
}
